//! 文件操作工具

use std::fs;
use std::path::Path;

/// 读取文件内容为字符串
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::file_utils::read_file_to_string;
/// use std::fs;
/// 
/// // 创建一个临时文件用于测试
/// fs::write("temp_test.txt", "Hello, world!").unwrap();
/// 
/// let content = read_file_to_string("temp_test.txt").unwrap();
/// assert_eq!(content, "Hello, world!");
/// 
/// // 清理临时文件
/// std::fs::remove_file("temp_test.txt").unwrap();
/// ```
pub fn read_file_to_string<P: AsRef<Path>>(path: P) -> Result<String, std::io::Error> {
    fs::read_to_string(path)
}

/// 将字符串写入文件
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::file_utils::write_string_to_file;
/// 
/// write_string_to_file("temp_test.txt", "Hello, world!").unwrap();
/// 
/// let content = std::fs::read_to_string("temp_test.txt").unwrap();
/// assert_eq!(content, "Hello, world!");
/// 
/// // 清理临时文件
/// std::fs::remove_file("temp_test.txt").unwrap();
/// ```
pub fn write_string_to_file<P: AsRef<Path>>(path: P, content: &str) -> Result<(), std::io::Error> {
    fs::write(path, content)
}

/// 检查文件是否存在
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::file_utils::file_exists;
/// use std::fs;
/// 
/// // 创建一个临时文件用于测试
/// fs::write("temp_test.txt", "Hello, world!").unwrap();
/// 
/// assert_eq!(file_exists("temp_test.txt"), true);
/// assert_eq!(file_exists("nonexistent.txt"), false);
/// 
/// // 清理临时文件
/// std::fs::remove_file("temp_test.txt").unwrap();
/// ```
pub fn file_exists<P: AsRef<Path>>(path: P) -> bool {
    Path::new(path.as_ref()).exists()
}

/// 获取文件大小（以字节为单位）
/// 
/// # 示例
/// 
/// ```
/// use rust_utils::file_utils::get_file_size;
/// use std::fs;
/// 
/// // 创建一个临时文件用于测试
/// fs::write("temp_test.txt", "Hello").unwrap(); // "Hello" 是 5 个字节
/// 
/// let size = get_file_size("temp_test.txt").unwrap();
/// assert_eq!(size, 5);
/// 
/// // 清理临时文件
/// std::fs::remove_file("temp_test.txt").unwrap();
/// ```
pub fn get_file_size<P: AsRef<Path>>(path: P) -> Result<u64, std::io::Error> {
    let metadata = fs::metadata(path)?;
    Ok(metadata.len())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn test_read_write_file() {
        let test_file = "test_file.txt";
        let test_content = "Hello, Rust utils!";
        
        // 写入文件
        assert!(write_string_to_file(test_file, test_content).is_ok());
        
        // 读取文件
        let content = read_file_to_string(test_file).unwrap();
        assert_eq!(content, test_content);
        
        // 清理测试文件
        fs::remove_file(test_file).unwrap();
    }

    #[test]
    fn test_file_exists() {
        let test_file = "test_file_exists.txt";
        assert_eq!(file_exists(test_file), false);
        
        // 创建文件
        fs::write(test_file, "test").unwrap();
        assert_eq!(file_exists(test_file), true);
        
        // 清理测试文件
        fs::remove_file(test_file).unwrap();
        assert_eq!(file_exists(test_file), false);
    }

    #[test]
    fn test_get_file_size() {
        let test_file = "test_file_size.txt";
        let test_content = "Hello"; // 5 bytes
        
        // 创建文件
        fs::write(test_file, test_content).unwrap();
        
        // 获取文件大小
        let size = get_file_size(test_file).unwrap();
        assert_eq!(size, 5);
        
        // 清理测试文件
        fs::remove_file(test_file).unwrap();
    }
}